﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MP.LayerHelper.MP_OBS.BLL;

public partial class Member_Control_MenuManager : System.Web.UI.UserControl
{
    public string MenuId
    {
        get { return Request["m"]; }
    }

    public DataTable tblMenu;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadMenu();
            loadCombo();
            loadData();
        }
    }

    void loadCombo()
    {
        DataTable dst = MenuManager.CreateInstant().SelectByParentIdRDT(null);
        dst.DefaultView.Sort = "MenuName";
        ddlMenu.DataTextField = "MenuName";
        ddlMenu.DataValueField = "Id";
        ddlMenu.DataSource = dst.DefaultView;
        ddlMenu.DataBind();

        ddlMenu.SelectedValue = MenuId;
    }

    void loadData()
    {
        if (!string.IsNullOrEmpty(MenuId))
        {
            DataTable tbl = MenuManager.CreateInstant().SelectByParentIdRDT(MenuId);
            tbl.DefaultView.Sort = "OrderIndex";
            rep.DataSource = tbl.DefaultView;
            rep.DataBind();
        }
    }

    void loadMenu()
    {
        tblMenu = MenuManager.CreateInstant().SelectByParentIdRDT(null);
        tblMenu.DefaultView.Sort = "MenuName";
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (rep.Items.Count > 0)
        {
            for (int i = 0; i < rep.Items.Count; i++)
            {
                string menuId = ((HiddenField)rep.Items[i].FindControl("hidId")).Value;
                string parentId = ((DropDownList)rep.Items[i].FindControl("ddlFuncMenu")).SelectedValue;
                CheckBox chkEnable = (CheckBox)rep.Items[i].FindControl("chkEnable");
                TextBox txtMenuName = (TextBox)rep.Items[i].FindControl("txtMenuName");
                TextBox txtUrl = (TextBox)rep.Items[i].FindControl("txtUrl");
                HiddenField hidDispOrder = (HiddenField)rep.Items[i].FindControl("hidDispOrder");

                MenuManager.CreateInstant().UpdateMenu(menuId, txtMenuName.Text.Trim(), txtUrl.Text.Trim(), int.Parse(hidDispOrder.Value), chkEnable.Checked, parentId);
            }

            Response.Redirect(Request.RawUrl);
        }
    }

    protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;
        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlFuncMenu");
        ddl.SelectedValue = row["ParentId"].ToString();
    }
}
